/*
 * Copyright (C) 2020 The LINN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.juqimiao.linn.logging;


import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import java.util.logging.Level;

/**
 * 封装 {@link java.util.logging.Logger} 的 Proxy。
 *
 * @author Collin Cai
 */
public class CommonLogger extends Logger {

    private java.util.logging.Logger logger;
    private String targetClzName;

    public CommonLogger(Class<?> clz) {
        super(clz);
        logger = java.util.logging.Logger.getLogger(clz.getName());
        targetClzName = clz.getName();
    }

    @Override
    public void debug(String message) {
        if (Logger.loggingEnabled) {
            logger.log(Level.SEVERE, message);
        }
    }

    @Override
    public void debug(String message, Throwable throwable) {
        if (Logger.loggingEnabled) {
            logger.log(Level.SEVERE, message);
            throwable.printStackTrace();
        }
    }

    @Override
    public void error(String message, Throwable throwable) {
        if (Logger.loggingEnabled) {
            logger.log(Level.SEVERE, String.format("%s: %s", targetClzName, message));
            throwable.printStackTrace();
        }
    }

    @Override
    public void info(String message) {
        if (Logger.loggingEnabled) {
            logger.log(Level.INFO, String.format("%s: %s", targetClzName, message));
        }
    }

    @Override
    public void info(String message, Throwable throwable) {
        if (Logger.loggingEnabled) {
            logger.log(Level.INFO, String.format("%s: %s", targetClzName, message));
            throwable.printStackTrace();
        }
    }

    @Override
    public void warn(String message) {
        if (Logger.loggingEnabled) {
            logger.log(Level.WARNING, String.format("%s: %s", targetClzName, message));
        }
    }

    @Override
    public void warn(String message, Throwable throwable) {
        if (Logger.loggingEnabled) {
            logger.log(Level.WARNING, String.format("%s: %s", targetClzName, message));
            throwable.printStackTrace();
        }
    }
}
